﻿#region Copyright (c) 2013-06, Olaf Kober <amarok.projects@gmail.com>
//================================================================================
//	Permission is hereby granted, free of charge, to any person obtaining a copy
//	of this software and associated documentation files (the "Software"), to deal
//	in the Software without restriction, including without limitation the rights
//	to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//	copies of the Software, and to permit persons to whom the Software is
//	furnished to do so, subject to the following conditions:
//
//	The above copyright notice and this permission notice shall be included in
//	all copies or substantial portions of the Software.
//
//	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//	IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//	FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//	AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//	LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//	OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//	THE SOFTWARE.
//================================================================================
#endregion

using System;
using System.Windows;


namespace Amarok.Presentation
{
	/// <summary>
	/// This type represents a window's position.
	/// </summary>
	public class WindowPosition
	{
		/// <summary>
		/// The position of the window's left edge, in relation to the desktop.
		/// </summary>
		public Double Left
		{
			get;
			set;
		}

		/// <summary>
		/// The position of the window's top edge, in relation to the desktop.
		/// </summary>
		public Double Top
		{
			get;
			set;
		}

		/// <summary>
		/// The width of the window.
		/// </summary>
		public Double Width
		{
			get;
			set;
		}

		/// <summary>
		/// The height of the window.
		/// </summary>
		public Double Height
		{
			get;
			set;
		}

		/// <summary>
		/// The bounds of the window.
		/// </summary>
		public Rect Bounds
		{
			get
			{
				return new Rect(this.Left, this.Top, this.Width, this.Height);
			}
			set
			{
				this.Left = value.Left;
				this.Top = value.Top;
				this.Width = value.Width;
				this.Height = value.Height;
			}
		}

		/// <summary>
		/// A boolean value indicating whether the window is maximized.
		/// </summary>
		public Boolean IsMaximized
		{
			get;
			set;
		}


		/// <summary>
		/// Initializes a new instance.
		/// </summary>
		public WindowPosition()
		{
		}

		/// <summary>
		/// Initializes a new instance.
		/// </summary>
		/// 
		/// <param name="left">
		/// The position of the window's left edge, in relation to the desktop.</param>
		/// <param name="top">
		/// the position of the window's top edge, in relation to the desktop.</param>
		/// <param name="width">
		/// The width of the Window.</param>
		/// <param name="height">
		/// The height of the window.</param>
		/// <param name="isMaximized">
		/// A boolean value indicating whether the window is maximized.</param>
		public WindowPosition(Double left, Double top, Double width, Double height, Boolean isMaximized)
		{
			this.Left = left;
			this.Top = top;
			this.Width = width;
			this.Height = height;
			this.IsMaximized = isMaximized;
		}

		/// <summary>
		/// Initializes a new instance.
		/// </summary>
		/// 
		/// <param name="bounds">
		/// The bounds of the window.</param>
		/// <param name="isMaximized">
		/// A boolean value indicating whether the window is maximized.</param>
		public WindowPosition(Rect bounds, Boolean isMaximized)
		{
			this.Left = bounds.Left;
			this.Top = bounds.Top;
			this.Width = bounds.Width;
			this.Height = bounds.Height;
			this.IsMaximized = isMaximized;
		}


		/// <summary>
		/// This method shrinks the window width and height, if necessary, to the current virtual screen size.
		/// </summary>
		public void SizeIntoFit()
		{
			if (this.Width > SystemParameters.VirtualScreenWidth)
				this.Width = SystemParameters.VirtualScreenWidth;

			if (this.Height > SystemParameters.VirtualScreenHeight)
				this.Height = SystemParameters.VirtualScreenHeight;
		}

		/// <summary>
		/// This method moves the window, if necessary, into the currently visible virtual screen.
		/// </summary>
		public void MoveIntoView()
		{
			if (this.Left < SystemParameters.VirtualScreenLeft - this.Width / 2)
				this.Left = SystemParameters.VirtualScreenLeft;

			if (this.Left + this.Width / 2 > SystemParameters.VirtualScreenWidth)
				this.Left = SystemParameters.VirtualScreenWidth - this.Width;

			if (this.Top < SystemParameters.VirtualScreenTop - this.Height / 2)
				this.Top = SystemParameters.VirtualScreenTop;

			if (this.Top + this.Height / 2 > SystemParameters.VirtualScreenHeight)
				this.Top = SystemParameters.VirtualScreenHeight - this.Height;
		}

	}
}
